package EnterpriseAlgorithm.Stack_Queue.Code.RealizeBetweenS_And_Q;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

class MyStack_iii {
    Queue<Integer> queue;

    public MyStack_iii() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.add(x);
    }

    public int pop() {
        if (queue.isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        } else {
            rePosition();
            return queue.poll();
        }
    }

    public int top() {
        if (queue.isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        } else {
            rePosition();
            int result = queue.poll();
            queue.add(result);
            return result;
        }
    }

    public boolean empty() {
        return queue.isEmpty();
    }

    public void rePosition() {
        int size = queue.size();
        size--;
        while (size-- > 0)
            queue.add(queue.poll());
    }
}
